All files / src/app/admin page.tsx

0% Statements 0/65
0% Branches 0/53
0% Functions 0/43
0% Lines 0/45

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156                                                                                                                                                                                                                                                                                                                       
'use client';
 
import { useEffect, useMemo, Suspense, lazy } from 'react';
import { useAuth } from '@/contexts/AuthContext';
import { useSearchParams, useRouter } from 'next/navigation';
import { useBannedRedirect } from '@/hooks/useBannedRedirect';
import { Loader2 } from 'lucide-react';
 
import { AdminRoute } from '@/components/auth/ProtectedRoute';
import AdminLayout from '@/components/layout/AdminLayout';
import { useTranslation } from 'react-i18next';
 
// Lazy load heavy components
const AdminOverview = lazy(() => import('@/components/admin/AdminOverview'));
const ResellerManagement = lazy(() => import('@/components/admin/ResellerManagement'));
const AdminEndUserManagement = lazy(() => import('@/components/admin/EndUserManagement'));
const ContentManagement = lazy(() => import('@/components/admin/ContentManagement'));
const GiftCodesManagement = lazy(() => import('@/components/admin/GiftCodesManagement'));
const DemosManagement = lazy(() => import('@/components/admin/DemosManagement'));
const SystemConfiguration = lazy(() => import('@/components/admin/SystemConfiguration'));
const CreditsPricingConfig = lazy(() => import('@/components/admin/CreditsPricingConfig'));
const SettingsPage = lazy(() => import('@/components/admin/SettingsPage'));
const DeviceManagementInterface = lazy(() => import('@/components/device/DeviceManagementInterface'));
const ActivityLogs = lazy(() => import('@/components/admin/ActivityLogs'));
const TicketManagement = lazy(() => import('@/components/admin/TicketManagement'));
const SecurityPanel = lazy(() => import('@/components/admin/SecurityPanel'));
const MasterApiKeyManagement = lazy(() => import('@/components/admin/MasterApiKeyManagement'));
const EpgManagement = lazy(() => import('@/components/admin/EpgManagement'));
const MediaScannerSimple = lazy(() => import('@/components/admin/MediaScannerSimple'));
const BillingDashboard = lazy(() => import('@/components/admin/BillingDashboard'));
const EmailSettings = lazy(() => import('@/components/admin/EmailSettings'));
const AppVersionsManagement = lazy(() => import('@/components/admin/AppVersionsManagement'));
 
// Loading fallback component
function TabLoadingFallback() {
  const { t } = useTranslation();
  return (
    <div className="flex items-center justify-center py-20">
      <div className="text-center">
        <Loader2 className="h-8 w-8 animate-spin text-blue-500 mx-auto mb-4" />
        <p className="text-slate-400">{t('common.loading')}</p>
      </div>
    </div>
  );
}
 
 
function AdminDashboardContent() {
  const { user, isAuthenticated, isLoading } = useAuth();
  const searchParams = useSearchParams();
  const router = useRouter();
  const activeTab = searchParams?.get('tab') ?? 'overview';
  const { t } = useTranslation();
 
  // Redirect banned users
  useBannedRedirect();
 
  // Redirect to login if not authenticated (after loading completes)
  useEffect(() => {
    if (!isLoading && !isAuthenticated) {
      router.replace('/login?redirect=/admin');
    }
  }, [isLoading, isAuthenticated, router]);
 
  const breadcrumbMap: Record<string, { label: string; href?: string }[]> = useMemo(() => ({
    overview: [{ label: t('breadcrumb.admin'), href: '/admin' }, { label: t('sidebar.overview') }],
    resellers: [{ label: t('breadcrumb.admin'), href: '/admin' }, { label: t('sidebar.resellers') }],
    users: [{ label: t('breadcrumb.admin'), href: '/admin' }, { label: t('sidebar.users') }],
    demos: [{ label: t('breadcrumb.admin'), href: '/admin' }, { label: t('sidebar.demos') }],
    content: [{ label: t('breadcrumb.admin'), href: '/admin' }, { label: t('sidebar.content') }],
    'media-scanner': [{ label: t('breadcrumb.admin'), href: '/admin' }, { label: t('sidebar.media-scanner') }],
    'gift-codes': [{ label: t('breadcrumb.admin'), href: '/admin' }, { label: t('sidebar.gift-codes') }],
    devices: [{ label: t('breadcrumb.admin'), href: '/admin' }, { label: t('sidebar.devices') }],
    tickets: [{ label: t('breadcrumb.admin'), href: '/admin' }, { label: t('sidebar.tickets') }],
    logs: [{ label: t('breadcrumb.admin'), href: '/admin' }, { label: t('sidebar.logs') }],
    security: [{ label: t('breadcrumb.admin'), href: '/admin' }, { label: t('sidebar.security') }],
    'master-keys': [{ label: t('breadcrumb.admin'), href: '/admin' }, { label: t('sidebar.master-keys') }],
    billing: [{ label: t('breadcrumb.admin'), href: '/admin' }, { label: t('sidebar.billing') }],
    email: [{ label: t('breadcrumb.admin'), href: '/admin' }, { label: t('sidebar.email') }],
    'app-versions': [{ label: t('breadcrumb.admin'), href: '/admin' }, { label: t('sidebar.app-versions') }],
    settings: [{ label: t('breadcrumb.admin'), href: '/admin' }, { label: t('sidebar.settings') }],
    epg: [{ label: t('breadcrumb.admin'), href: '/admin' }, { label: t('sidebar.epg') }]
  }), [t]);
 
  const isReady = !isLoading && isAuthenticated && user?.role === 'admin';
 
  // Early returns to prevent re-rendering
  if (isLoading) {
    return (
      <div className="min-h-screen flex items-center justify-center">
        <div className="text-lg text-foreground">{t('common.loading')}</div>
      </div>
    );
  }
 
  if (!isAuthenticated) {
    // Will redirect via useEffect, show loading in the meantime
    return (
      <div className="min-h-screen flex items-center justify-center">
        <Loader2 className="h-8 w-8 animate-spin text-blue-500" />
      </div>
    );
  }
 
  if (user?.role !== 'admin') {
    return (
      <div className="min-h-screen flex items-center justify-center">
        <div className="text-lg text-destructive">{t('admin.dashboard.accessDenied')}</div>
      </div>
    );
  }
 
  if (!isReady) {
    return (
      <div className="min-h-screen flex items-center justify-center">
        <div className="text-lg text-foreground">{t('admin.dashboard.preparing')}</div>
      </div>
    );
  }
 
  return (
    <AdminRoute>
      <AdminLayout breadcrumbs={breadcrumbMap[activeTab] ?? [{ label: t('breadcrumb.admin'), href: '/admin' }]}>
        <Suspense fallback={<TabLoadingFallback />}>
          {activeTab === 'overview' && <AdminOverview />}
          {activeTab === 'resellers' && <ResellerManagement />}
          {activeTab === 'users' && <AdminEndUserManagement />}
          {activeTab === 'content' && <ContentManagement />}
          {activeTab === 'media-scanner' && <MediaScannerSimple />}
          {activeTab === 'demos' && <DemosManagement />}
          {activeTab === 'devices' && <DeviceManagementInterface />}
          {activeTab === 'tickets' && <TicketManagement />}
          {activeTab === 'logs' && <ActivityLogs />}
          {activeTab === 'security' && <SecurityPanel />}
          {activeTab === 'master-keys' && <MasterApiKeyManagement />}
          {activeTab === 'billing' && <BillingDashboard />}
          {activeTab === 'email' && <EmailSettings />}
          {activeTab === 'settings' && <SettingsPage />}
          {activeTab === 'epg' && <EpgManagement />}
          {activeTab === 'gift-codes' && <GiftCodesManagement />}
          {activeTab === 'app-versions' && <AppVersionsManagement />}
        </Suspense>
      </AdminLayout>
    </AdminRoute>
  );
}
 
export default function AdminDashboard() {
  const { t } = useTranslation();
  return (
    <Suspense fallback={<div className="min-h-screen flex items-center justify-center">{t('common.loading')}</div>}>
      <AdminDashboardContent />
    </Suspense>
  );
}